package com.shepin.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.shepin.R;
import com.shepin.activity.ShePinBeautyActivity;
import com.shepin.activity.ShePinCigarActivity;
import com.shepin.activity.ShePinMemberActivity;
import com.shepin.activity.ShePinOneDollarActivity;
import com.shepin.activity.ShePinOnlineActivity;
import com.shepin.activity.ShePinOnlineAuctionActivity;
import com.shepin.activity.ShePinOverseasActivity;
import com.shepin.activity.ShePinRecommend;
import com.shepin.adapter.AvdAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by apple on 16/7/29.
 */
public class IndexActivity extends Fragment implements View.OnClickListener{
    private ImageView[] imageViews = null;
    private ImageView imageView = null;
    private ViewPager viewPager = null;
    private AtomicInteger what = new AtomicInteger(0);
    private boolean isContinue = true;
    private Context context;
    private ImageView shepin_recommand;
    private ImageView shepin_beauty;
    private ImageView shepin_online;
    private ImageView shepin_member;

    private ImageView shepin_onlineauction;
    private ImageView shepin_overseas;
    private ImageView shepin_cigar;
    private ImageView onedollar;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.index,null);
        initView(view);
        return view;
    }

    public void initView(View view) {
        this.context=IndexActivity.this.getActivity();
        viewPager = (ViewPager)view.findViewById(R.id.index_viewpager);
        ViewGroup group = (ViewGroup) view.findViewById(R.id.index_group_dot);
        List<View> advPics = new ArrayList<View>();

        shepin_recommand=(ImageView)view.findViewById(R.id.shepin_recommand);
        shepin_recommand.setOnClickListener(this);
        shepin_beauty=(ImageView)view.findViewById(R.id.shepin_beauty);
        shepin_beauty.setOnClickListener(this);
        shepin_online=(ImageView)view.findViewById(R.id.shepin_online);
        shepin_online.setOnClickListener(this);
        shepin_member=(ImageView)view.findViewById(R.id.shepin_member);
        shepin_member.setOnClickListener(this);


        shepin_onlineauction=(ImageView)view.findViewById(R.id.shepin_onlineauction);
        shepin_onlineauction.setOnClickListener(this);
        shepin_overseas=(ImageView)view.findViewById(R.id.shepin_overseas);
        shepin_overseas.setOnClickListener(this);
        shepin_cigar=(ImageView)view.findViewById(R.id.shepin_cigar);
        shepin_cigar.setOnClickListener(this);
        onedollar=(ImageView)view.findViewById(R.id.onedollar);
        onedollar.setOnClickListener(this);











        ImageView img1 = new ImageView(context);
        img1.setBackgroundResource(R.drawable.banner1);
        advPics.add(img1);

        ImageView img2 = new ImageView(context);
        img2.setBackgroundResource(R.drawable.banner2);
        advPics.add(img2);

        ImageView img3 = new ImageView(context);
        img3.setBackgroundResource(R.drawable.banner3);
        advPics.add(img3);
        imageViews = new ImageView[advPics.size()];

        for (int i = 0; i < advPics.size(); i++) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new LayoutParams(20, 20));
            imageView.setPadding(5, 5, 5, 5);
            imageViews[i] = imageView;
            if (i == 0) {
                imageViews[i].setBackgroundResource(R.drawable.guide_dot_select);
            } else {
                imageViews[i].setBackgroundResource(R.drawable.guide_dot_normal);
            }
            group.addView(imageViews[i]);
        }
        viewPager.setAdapter(new AvdAdapter(advPics));
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
        viewPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isContinue = false;
                        break;
                    case MotionEvent.ACTION_UP:
                        isContinue = true;
                        break;
                    default:
                        isContinue = true;
                        break;
                }
                return false;
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    if (isContinue) {
                        viewHandler.sendEmptyMessage(what.get());
                        whatOption();
                    }
                }
            }

        }).start();
    }

        private void whatOption() {
            what.incrementAndGet();
            if (what.get() > imageViews.length - 1) {
                what.getAndAdd(-4);
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {

            }
        }

    private final Handler viewHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            viewPager.setCurrentItem(msg.what);
            super.handleMessage(msg);
        }

    };
    private final class GuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            what.getAndSet(arg0);
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0].setBackgroundResource(R.drawable.guide_dot_select);
                if (arg0 != i) {
                    imageViews[i].setBackgroundResource(R.drawable.guide_dot_normal);
                }
            }

        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.shepin_recommand:
                Intent intentrecommand=new Intent(IndexActivity.this.getActivity(), ShePinRecommend.class);
                startActivity(intentrecommand);
                break;
            case R.id.shepin_beauty:
                Intent intentbeauty=new Intent(IndexActivity.this.getActivity(), ShePinBeautyActivity.class);
                startActivity(intentbeauty);
                break;
            case R.id.shepin_online:
                Intent intentonline=new Intent(IndexActivity.this.getActivity(), ShePinOnlineActivity.class);
                startActivity(intentonline);
                break;
            case R.id.shepin_member:
                Intent intentmember=new Intent(IndexActivity.this.getActivity(), ShePinMemberActivity.class);
                startActivity(intentmember);
                break;

            case R.id.shepin_onlineauction:
                Intent intentonlineauction=new Intent(IndexActivity.this.getActivity(), ShePinOnlineAuctionActivity.class);
                startActivity(intentonlineauction);
                break;
            case R.id.shepin_overseas:
                Intent intentoverseas=new Intent(IndexActivity.this.getActivity(), ShePinOverseasActivity.class);
                startActivity(intentoverseas);
                break;
            case R.id.shepin_cigar:
                Intent intentcigar=new Intent(IndexActivity.this.getActivity(), ShePinCigarActivity.class);
                startActivity(intentcigar);
                break;
            case R.id.onedollar:
                Intent intentonedollar=new Intent(IndexActivity.this.getActivity(), ShePinOneDollarActivity.class);
                startActivity(intentonedollar);
                break;
        }
    }
}
